package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.cs157a.controller.AposApplicationController;
import com.cs157a.dao.PartType;
import com.cs157a.dao.RdiPart;

/**
 * This class shows the dialog for input new part.
 * 
 * @author hhong
 * 
 */
public class NewPartDialog extends JDialog {

	private static final long serialVersionUID = 20012100301L;
	private static final int FIELD_WIDTH = 20;

	private JButton saveButton;
	private JButton cancelButton;

	private JLabel vendorLabel;
	private JComboBox vendorCombo;
	private JLabel partNumberLabel;
	private JTextField partNumberField;
	private JLabel coreLabel;
	private JTextField coreField;

	private JLabel inHeadLabel;
	private JTextField inHeadField;
	private JLabel outHeadLabel;
	private JTextField outHeadField;
	private JLabel inConLabel;
	private JTextField inConField;
	private JLabel outConLabel;
	private JTextField outConField;
	private JLabel tMountLabel;
	private JTextField tMountField;
	private JLabel oilCoolLabel;
	private JTextField oilCoolField;
	private JLabel priceLabel;
	private JTextField priceField;

	private JLabel amountLabel;
	private JTextField amountField;
	private JLabel typeLabel;
	private JComboBox typeCombo;

	private AposApplicationController controller;

	public NewPartDialog(final AposApplicationController controller) {
		super();
		this.controller = controller;

		setPreferredSize(new Dimension(400, 500));

		saveButton = new JButton(CommonString.SAVE);
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (partNumberField.getText().isEmpty()
						|| partNumberField.getText().length() > 6
						|| coreField.getText().isEmpty()
						|| coreField.getText().length() > 32
						|| inConField.getText().isEmpty()
						|| inConField.getText().length() > 14
						|| outConField.getText().isEmpty()
						|| outConField.getText().length() > 14
						|| inHeadField.getText().isEmpty()
						|| inHeadField.getText().length() > 17
						|| outHeadField.getText().isEmpty()
						|| outHeadField.getText().length() > 17
						|| oilCoolField.getText().isEmpty()
						|| oilCoolField.getText().length() > 10
						|| priceField.getText().isEmpty()
						|| priceField.getText().length() > 8
						|| tMountField.getText().isEmpty()
						|| tMountField.getText().length() > 24
						|| amountField.getText().isEmpty()
						|| amountField.getText().length() > 10)
					JOptionPane
							.showMessageDialog(rootPane,
									"Please Enter All the Fields! \n Part Number with Maximum 6 digits");
				else {
					RdiPart part = getPart();
					controller.savePart(part);
					setVisible(false);
					clearAllFields();
				}
			}
		});
		cancelButton = new JButton(CommonString.CANCEL);
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				clearAllFields();
			}
		});
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(saveButton);
		buttonPanel.add(cancelButton);

		add(createInnerPanel(), BorderLayout.NORTH);
		add(buttonPanel, BorderLayout.SOUTH);
		setModalityType(ModalityType.APPLICATION_MODAL);
	}

	protected void clearAllFields() {
		// TODO Auto-generated method stub
		coreField.setText("");
		inConField.setText("");
		outConField.setText("");
		inHeadField.setText("");
		outHeadField.setText("");
		oilCoolField.setText("");
		partNumberField.setText("");
		priceField.setText("");
		tMountField.setText("");
		amountField.setText("");
	}

	protected RdiPart getPart() {
		// TODO Auto-generated method stub
		RdiPart part = new RdiPart();
		if (amountField.getText().isEmpty()) {
			part.setAmount(0);
		} else {
			part.setAmount(Integer.parseInt(amountField.getText()));
		}

		part.setCore(coreField.getText());
		part.setInCon(inConField.getText());
		part.setOuCon(outConField.getText());
		part.setInHead(inHeadField.getText());
		part.setOutHead(outHeadField.getText());
		part.setOilCool(oilCoolField.getText());
		part.setPartNumber(partNumberField.getText());
		part.setPrice(Double.parseDouble(priceField.getText()));
		part.setTMount(tMountField.getText());
		part.setType((PartType) typeCombo.getSelectedItem());
		part.setVendor(vendorCombo.getSelectedItem().toString());

		return part;
	}

	private JPanel createInnerPanel() {

		vendorLabel = new JLabel(PartColumn.VENDOR.getName());
		vendorCombo = new JComboBox(VendorType.values());

		partNumberLabel = new JLabel(PartColumn.PART_NUMBER.getName());
		partNumberField = new JTextField(FIELD_WIDTH);

		coreLabel = new JLabel(PartColumn.CORE.getName());
		coreField = new JTextField(FIELD_WIDTH);

		inHeadLabel = new JLabel(PartColumn.IN_HEAD.getName());
		outHeadLabel = new JLabel(PartColumn.OUT_HEAD.getName());
		inConLabel = new JLabel(PartColumn.IN_CON.getName());
		outConLabel = new JLabel(PartColumn.OUT_CON.getName());
		tMountLabel = new JLabel(PartColumn.T_MOUNT.getName());
		oilCoolLabel = new JLabel(PartColumn.OIL_COOL.getName());
		priceLabel = new JLabel(PartColumn.PRICE.getName());
		amountLabel = new JLabel(PartColumn.AMOUNT.getName());
		typeLabel = new JLabel(PartColumn.TYPE.getName());

		inHeadField = new JTextField(FIELD_WIDTH);
		outHeadField = new JTextField(FIELD_WIDTH);
		inConField = new JTextField(FIELD_WIDTH);
		outConField = new JTextField(FIELD_WIDTH);
		tMountField = new JTextField(FIELD_WIDTH);
		coreField = new JTextField(FIELD_WIDTH);
		oilCoolField = new JTextField(FIELD_WIDTH);
		priceField = new JTextField(FIELD_WIDTH);
		amountField = new JTextField(FIELD_WIDTH);
		typeCombo = new JComboBox(PartType.values());

		clearAllFields();

		JPanel innerPanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.fill = GridBagConstraints.HORIZONTAL;

		innerPanel.add(vendorLabel, c);

		c.gridx = 0;
		c.gridy = 1;
		innerPanel.add(partNumberLabel, c);

		c.gridx = 0;
		c.gridy = 2;
		innerPanel.add(coreLabel, c);

		c.gridx = 0;
		c.gridy = 3;
		innerPanel.add(inHeadLabel, c);

		c.gridx = 0;
		c.gridy = 4;
		innerPanel.add(outHeadLabel, c);

		c.gridx = 0;
		c.gridy = 5;
		innerPanel.add(inConLabel, c);
		c.gridx = 0;
		c.gridy = 6;
		innerPanel.add(outConLabel, c);

		c.gridx = 0;
		c.gridy = 7;
		innerPanel.add(tMountLabel, c);
		c.gridx = 0;
		c.gridy = 8;
		innerPanel.add(oilCoolLabel, c);
		c.gridx = 0;
		c.gridy = 9;
		innerPanel.add(priceLabel, c);
		c.gridx = 0;
		c.gridy = 10;
		innerPanel.add(amountLabel, c);
		c.gridx = 0;
		c.gridy = 11;
		innerPanel.add(typeLabel, c);

		c.gridx = 1;
		c.gridy = 0;
		innerPanel.add(vendorCombo, c);

		c.gridx = 1;
		c.gridy = 1;
		innerPanel.add(partNumberField, c);

		c.gridx = 1;
		c.gridy = 2;
		innerPanel.add(coreField, c);

		c.gridx = 1;
		c.gridy = 3;
		innerPanel.add(inHeadField, c);

		c.gridx = 1;
		c.gridy = 4;
		innerPanel.add(outHeadField, c);

		c.gridx = 1;
		c.gridy = 5;
		innerPanel.add(inConField, c);

		c.gridx = 1;
		c.gridy = 6;
		innerPanel.add(outConField, c);

		c.gridx = 1;
		c.gridy = 7;
		innerPanel.add(tMountField, c);

		c.gridx = 1;
		c.gridy = 8;
		innerPanel.add(oilCoolField, c);

		c.gridx = 1;
		c.gridy = 9;
		innerPanel.add(priceField, c);

		c.gridx = 1;
		c.gridy = 10;
		innerPanel.add(amountField, c);

		c.gridx = 1;
		c.gridy = 11;
		innerPanel.add(typeCombo, c);

		return innerPanel;
	}

}
